package com.bougaga.samplespringcloud.gateway.filter;

import javax.servlet.http.HttpServletResponse;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;

/**
 * 
 * @author bougaga
 * 
 * error.status_code：错误编码
   error.exception：Exception异常对象
   error.message：错误信息
 *
 */
public class ExceptionFilter extends ZuulFilter{

	@Override
	public Object run() {
		RequestContext context = RequestContext.getCurrentContext();
		//this.helper.addIgnoredHeaders();
		try {
			testException();
		} catch (Exception e) {
			context.set("error.status_code", HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
			context.set("error.exception", e);
		}
		return null;
	}

	@Override
	public boolean shouldFilter() {
		
		return true;
	}

	@Override
	public int filterOrder() {

		return 1;
	}

	@Override
	public String filterType() {

		return "pre";
	}

	private void testException() {
        throw new RuntimeException("假装有个异常！");
    }
}
